<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\tinyinfo\controller;

use app\tinyinfo\model\Tinyinfo;
use think\Request;
use mip\Htmlp;
use think\Db;
use mip\AdminApiBase;

class ApiAdminTinyinfo extends AdminApiBase
{

    public function get_list(){

        $tinyinfo_code = input('post.tinyinfo_code');
        $page = input('post.page');
        $limit = input('post.limit');

        $where = array();
        if($tinyinfo_code&&$tinyinfo_code!=''){
            $where['tinyinfo_code']=$tinyinfo_code;
        }
        $order = null;

        $list = Tinyinfo::getList($where,$order,$page,$limit);

        foreach ($list as $k1 => $v1){
            if($v1['save_visit_numb']){
                $list[$k1]['strVisitNumb'] = $v1['visit_numb'].'次';
            }else{
                $list[$k1]['strVisitNumb'] = '不保存';
            }
        }

        $cnt = db('tinyinfo')->where($where)->count();

        return jsonSuccess('',array('list' => $list,'count'=>$cnt) );

    }

    public function get_by_id(){

        $id = input('post.id');

        $gg = Tinyinfo::getById($id);
        return jsonSuccess('获取短内容信息',$gg);

    }

    public function do_save(){
        $id = input('post.id');
        $tinyinfo_code = input('post.tinyinfo_code');
        $title = input('post.title');
        $description= input('post.description');
        $img_url = input('post.img_url');
        $link_url = input('post.link_url');
        $saveStatus = input('post.saveStatus');
        $status = input('post.status');

        $d1=array('tinyinfo_code'=>$tinyinfo_code,'title'=>$title,'description'=>$description
        ,'img_url'=>$img_url,'link_url'=>$link_url);

        if($status!=null){
            $d1['status'] = $status;
        }

        if($saveStatus=='add'){
            $res = model('app\tinyinfo\model\Tinyinfo')->doAdd($d1);
            if($res){
                return jsonSuccess('短内容新增保存成功。');
            }else{
                $lastSQL = \think\Db::getLastSql();
                $sqlError = \think\Db::getError();

                return jsonError('新增短内容失败： '.$lastSQL);
            }

        }

        if($saveStatus=='edit'){
            $res = model('app\tinyinfo\model\Tinyinfo')->doEdit($id , $d1);
            if($res){
                return jsonSuccess('短内容编辑保存成功。');
            }else{
                $lastSQL = \think\Db::getLastSql();
                $sqlError = \think\Db::getError();
                return jsonError('编辑短内容失败。Error： '.$sqlError.'  SQL ： '.$lastSQL);
            }

        }
        return jsonError('由于未知原因，保存失败');
    }

    public function do_del(){
        $id = input('post.id');
        $res = model('app\tinyinfo\model\Tinyinfo')->doDelById($id);
        if($res){
            return jsonSuccess('短内容删除成功。');
        }else{
            return jsonError("由于未知原因，短内容删除失败了。");
        }
    }

    public function do_sort_save()
    {
        $itemList = input('post.itemList/a');
        if ($itemList) {
            foreach ($itemList as $key => $val) {
                if ($itemListInfo = db('tinyinfo')->where('id',$val['id'])->find()) {
                    model('app\tinyinfo\model\Tinyinfo')->doEdit($val['id'] , array('sort' => $val['sort']));
                }
            }
            return jsonSuccess('保存成功');
        }
        return jsonError('未获得排序数据。');

    }

}